package com.haredot.config;

import com.haredot.utils.ApplicationUtils;
import com.haredot.websocket.ChatWebSocketHandler;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import javax.annotation.Resource;


@Configuration
@EnableWebSocket
public class WebSocketConfig {

    /**
     * @ServerEndpoint 注释的类 不能被 Spring 管理，所以导入无法注入 applicationEventPublisher
     *  applicationEventPublisher 是一个特殊的 Bean , 也无法通过 ApplicationContext.getBean方法直接获取
     */
    @Resource
    private ApplicationEventPublisher applicationEventPublisher ;

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        ServerEndpointExporter serverEndpointExporter = new ServerEndpointExporter();
        serverEndpointExporter.setAnnotatedEndpointClasses(ChatWebSocketHandler.class);
        ApplicationUtils.setApplicationEventPublisher(applicationEventPublisher);
        return serverEndpointExporter ;
    }
}
